package com.framework.core.exception;

import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

/**
 * Exception工具类
 * @author  fusf
 */
public class ExceptionUtil {
	/**
	 * 
	 * 获取异常定义错误代码
	 * @param ex
	 * @return
	 */
	public  static String getErrorCode(Exception ex) {

		String errorCode = "";
		if (ex instanceof NoSuchRequestHandlingMethodException) {
			errorCode =  "error_1001";
		} else if (ex instanceof HttpRequestMethodNotSupportedException) {
			errorCode ="error_1002";
		} else if (ex instanceof HttpMediaTypeNotSupportedException) {
			errorCode = "error_1003";
		} else if (ex instanceof TypeMismatchException) {
			errorCode ="error_1201";
		} else if (ex instanceof HttpMessageNotWritableException) {
			errorCode ="error_1503";
		} else if (ex instanceof MethodArgumentNotValidException) {
			errorCode = "error_1202";
		} else if (ex instanceof MissingServletRequestPartException) {
			errorCode = "error_1501";
		} else if (ex instanceof BindException) {
			errorCode = "error_1502";
		} else if (ex instanceof NoHandlerFoundException) {
			errorCode = "error_1201";
		} else if (ex instanceof BusinessException) {
			errorCode = "";
		} else if(ex instanceof DataIntegrityViolationException) {
			errorCode = "error_2001";
		} else if(ex instanceof NullPointerException) {
			errorCode = "error_2002";
		}
		else {//其他错误码
			errorCode ="error_1999";
		}
		//more to add...        .
		return errorCode;
	}



}
